package com.lewis.module.m2.api.bill.code.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lewis.common.core.entity.page.PageResult;
import com.lewis.common.core.utils.StringUtil;
import com.lewis.module.m2.api.bill.code.entity.BillCode;
import com.lewis.module.m2.api.bill.code.mapper.BillCodeMapper;
import com.lewis.module.m2.api.bill.code.service.IBillCodeService;
import com.lewis.module.m2.api.bill.entity.Bill;
import com.lewis.module.m2.api.bill.mapper.BillMapper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * 账单编码Service业务层处理
 *
 * @author lewis
 * @date 2021-11-09
 */
@Service
public class BillCodeServiceImpl extends ServiceImpl<BillCodeMapper, BillCode> implements IBillCodeService {

	@Resource
	private BillCodeMapper billCodeMapper;

	@Resource
	private BillMapper billMapper;

	/**
	 * 查询账单编码
	 *
	 * @param billCode 账单编码主键
	 * @return 账单编码
	 */
	@Override
	public BillCode selectBillCodeByBillCode(Integer billCode) {
		return billCodeMapper.selectOne(
				new LambdaQueryWrapper<BillCode>()
						.eq(BillCode::getId, billCode)
		);
	}

	/**
	 * 查询账单编码列表
	 *
	 * @param billCode 账单编码
	 * @return 账单编码
	 */
	@Override
	public PageResult selectBillCodeList(BillCode billCode) {
		PageResult pageResult = this.getPageList(billCode);
		List<BillCode> billCodeList = (List<BillCode>) pageResult.getData();
		List<JSONObject> jsonObjects = new ArrayList<>();
		for (BillCode code : billCodeList) {
			JSONObject jsonObject = (JSONObject) JSONObject.toJSON(code);
			List<Bill> billList = billMapper.selectList(
					new LambdaQueryWrapper<Bill>()
							.eq(Bill::getBillCodeId, code.getId())
							.orderByDesc(Bill::getId)
			);

			BigDecimal incomeAll = new BigDecimal(0);
			BigDecimal payAll = new BigDecimal(0);

			for (Bill bill1 : billList) {
				if (bill1.getBillType() == 1) {
					incomeAll = incomeAll.add(new BigDecimal(Double.toString(bill1.getMoney())));
				} else {
					payAll = payAll.add(new BigDecimal(Double.toString(bill1.getMoney())));
				}
			}
			jsonObject.put("incomeAll", incomeAll);
			jsonObject.put("payAll", payAll);
			jsonObject.put("balanceAll", incomeAll.subtract(payAll));
			jsonObjects.add(jsonObject);
		}
		pageResult.setData(jsonObjects);
		return pageResult;
	}

	/**
	 * 查询账单编码列表
	 *
	 * @param billCode 账单编码
	 * @return 账单编码
	 */
	@Override
	public List<BillCode> selectList(BillCode billCode) {
		LambdaQueryWrapper<BillCode> lqw = new LambdaQueryWrapper<>();
		lqw.eq(StringUtil.isNotNull(billCode.getId()), BillCode::getId, billCode.getId());
		lqw.eq(StringUtil.isNotNull(billCode.getUserId()), BillCode::getUserId, billCode.getUserId());
		lqw.like(StringUtil.isNotNull(billCode.getBillName()), BillCode::getBillName, billCode.getBillName());
		lqw.eq(StringUtil.isNotNull(billCode.getIsDelete()), BillCode::getIsDelete, billCode.getIsDelete());
		lqw.orderByDesc(BillCode::getId);
		return billCodeMapper.selectList(lqw);
	}

	/**
	 * 查询账单编码列表
	 *
	 * @param billCode 账单编码
	 * @return 账单编码
	 */
	@Override
	public PageResult getPageList(BillCode billCode) {
		LambdaQueryWrapper<BillCode> lqw = new LambdaQueryWrapper<>();
		lqw.eq(StringUtil.isNotNull(billCode.getId()), BillCode::getId, billCode.getId());
		lqw.eq(StringUtil.isNotNull(billCode.getUserId()), BillCode::getUserId, billCode.getUserId());
		lqw.like(StringUtil.isNotNull(billCode.getBillName()), BillCode::getBillName, billCode.getBillName());
		lqw.eq(StringUtil.isNotNull(billCode.getIsDelete()), BillCode::getIsDelete, billCode.getIsDelete());
		lqw.orderByDesc(BillCode::getId);
		Page<BillCode> billCodePage = billCodeMapper.selectPage(new Page<>(billCode.getPageIndex(), billCode.getPageSize()), lqw);
		return PageResult.ok().setData(billCodePage.getRecords()).setTotal(billCodePage.getTotal());
	}

	/**
	 * 新增账单编码
	 *
	 * @param billCode 账单编码
	 * @return 结果
	 */
	@Override
	public int insertBillCode(BillCode billCode) {
		return billCodeMapper.insert(billCode);
	}

	/**
	 * 修改账单编码
	 *
	 * @param billCode 账单编码
	 * @return 结果
	 */
	@Override
	public int updateBillCode(BillCode billCode) {
		return billCodeMapper.updateById(billCode);
	}

	/**
	 * 批量删除账单编码
	 *
	 * @param billCodes 需要删除的账单编码主键
	 * @return 结果
	 */
	@Override
	public int deleteBillCodeByBillCodes(Integer[] billCodes) {
		List<Integer> ids = new ArrayList<>(Arrays.asList(billCodes));
		return billCodeMapper.deleteBatchIds(ids);
	}

	/**
	 * 删除账单编码信息
	 *
	 * @param id 账单编码主键
	 * @return 结果
	 */
	@Override
	public int deleteBillCodeByBillCode(Integer id) {
		return billCodeMapper.deleteById(id);
	}

}